//
//  SellOrderFinishDetailsViewController.m
//  HuiMWorld
//
//  Created by chirui on 2018/1/29.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "SellOrderFinishDetailsViewController.h"
#import "OrderTableViewCell.h"

@interface SellOrderFinishDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
}

@end

@implementation SellOrderFinishDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"订单详情")
    
    NSLog(@"dataDic==%@",self.dataDic);

    [self creatTableView];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[OrderTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[OrderTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray * array = self.dataDic[@"products"];
    
    return array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 210;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    
    UIView * commentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    commentView.backgroundColor = [UIColor whiteColor];
  
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
    label.text = @"评论内容";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:15];
    [commentView addSubview:label];
    
    UILabel * comment = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
    comment.text = self.dataDic[@"comment"][@"content"];
    comment.textColor = [UIColor blackColor];
    comment.font = [UIFont systemFontOfSize:15];
    comment.numberOfLines = 0;
    [comment sizeToFit];
    [commentView addSubview:comment];
    
      NSArray * imageArray = self.dataDic[@"comment"][@"img_srcs"];
    
    UIImageView * imageView;
    for (int i=0; i<imageArray.count; i++) {
        
        imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*i+(WIDTH-60)/3*i, CGRectGetMaxY(comment.frame)+10, (WIDTH-60)/3, (WIDTH-60)/3)];
        imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"img_takeout_shpu_1"]];
         [commentView addSubview:imageView];
    }
   
    
    UIView * replyView;
    if (imageArray.count>0) {
        
        replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+10, WIDTH, 50)];
    }else {
        
        replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(comment.frame)+10, WIDTH, 50)];
    }
    
    replyView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    UILabel * reply = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
    reply.text = @"平台回复";
    reply.textColor = NavBackColor;
    reply.font = [UIFont systemFontOfSize:15];
    [replyView addSubview:reply];
    
    UILabel * replyVolu = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
    replyVolu.text = self.dataDic[@"comment"][@"replay"];
    replyVolu.textColor = [UIColor grayColor];
    replyVolu.font = [UIFont systemFontOfSize:15];
    replyVolu.numberOfLines = 0;
    [replyVolu sizeToFit];
    [replyView addSubview:replyVolu];
    
    if (imageView) {
        
        replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+10, WIDTH, CGRectGetMaxY(replyVolu.frame)+10)];
    }else {
        
        replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(comment.frame)+10, WIDTH, CGRectGetMaxY(replyVolu.frame)+10)];
    }
    
    commentView.frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(replyView.frame));
    
    return CGRectGetMaxY(commentView.frame)+350;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = self.dataDic[@"products"][indexPath.row];
    OrderTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[OrderTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    [cell.goodsImage sd_setImageWithURL:ImageUrl(dic[@"cover_photo"]) placeholderImage:ImageName(@"")];
    cell.goodsName.text = dic[@"product_name"];
    cell.goodsName.frame = CGRectMake(98, 10, WIDTH -110, 20);
    cell.goodsName.numberOfLines = 2;
    [cell.goodsName sizeToFit];
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"price"]];
    cell.numberLabel.text = [NSString stringWithFormat:@"X%@",dic[@"number"]];
    
    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 210)];
    heardView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    
    UIView * statisView =[[UIView alloc]initWithFrame:CGRectMake((WIDTH-100)/2, 30, 100, 40)];
    statisView.backgroundColor = [UIColor clearColor];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 30, 30)];
    
    [statisView addSubview:imageView];
    
    if ([self.dataDic[@"status"] isEqualToString:@"done"]) {
        imageView.image = ImageName(@"icon_lab_ok");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor blackColor] andText:@"已完成" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    UIView * addressView =[[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH, 100)];
    addressView.backgroundColor =[UIColor whiteColor];
    
    
    UIImageView * addressImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 75/2, 25, 25)];
    addressImageView.image = ImageName(@"icon_order_add");
    [addressView addSubview:addressImageView];
    
    [self creatLabelFrame:CGRectMake(35, 20, 120, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"address"][@"name"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
    [self creatLabelFrame:CGRectMake(160, 20, WIDTH - 170, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"address"][@"phone"] andFont:15 andAliment:NSTextAlignmentRight onView:addressView];
    
    [self creatLabelFrame:CGRectMake(35, 60, WIDTH - 40, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"address"][@"full_addr"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
    [heardView addSubview:addressView];
    [heardView addSubview:statisView];
    
    
    return heardView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView * footerView = [[UIView alloc]initWithFrame:CGRectMake(0,0,WIDTH, 500)];
    footerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    
    UIView * commentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    commentView.backgroundColor = [UIColor whiteColor];
    [footerView addSubview:commentView];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
    label.text = @"评论内容:";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:15];
    [commentView addSubview:label];
    
    UILabel * comment = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
    comment.text = self.dataDic[@"comment"][@"content"];
    comment.textColor = [UIColor blackColor];
    comment.font = [UIFont systemFontOfSize:15];
    comment.numberOfLines = 0;
    [comment sizeToFit];
     [commentView addSubview:comment];
    
    UIImageView * imageView;
    
    NSArray * imageArray = self.dataDic[@"comment"][@"img_srcs"];
    
    for (int i=0; i<imageArray.count; i++) {
        
        imageView =[[UIImageView alloc]initWithFrame:CGRectMake(20+10*i+(WIDTH-60)/3*i, CGRectGetMaxY(comment.frame)+15, (WIDTH-60)/3, (WIDTH-60)/3)];
//        imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"img_takeout_shpu_1"]];
        [imageView sd_setImageWithURL:ImageUrl(imageArray[i]) placeholderImage:ImageName(@"")];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [imageView tapAction:^{
            
            TCLPhotoBrowserViewController *photo = [[TCLPhotoBrowserViewController alloc]init];
            photo.photos = imageArray;
            photo.currentPage = i;
            [self presentViewController:photo animated:YES completion:nil];
        }];
        
         [commentView addSubview:imageView];
    }
   
    
    UIView * replyView;
    if (imageArray.count>0) {
        
        replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+10, WIDTH, 50)];
    }else {
        
        replyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(comment.frame)+10, WIDTH, 50)];
    }
    
    replyView.backgroundColor = [UIColor groupTableViewBackgroundColor];
     [commentView addSubview:replyView];
    
    UILabel * reply = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)];
    reply.text = @"平台回复:";
    reply.textColor = NavBackColor;
    reply.font = [UIFont systemFontOfSize:15];
    [replyView addSubview:reply];
    
    UILabel * replyVolu = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, WIDTH - 110, 20)];
    replyVolu.text = self.dataDic[@"comment"][@"replay"];
    replyVolu.textColor = [UIColor grayColor];
    replyVolu.font = [UIFont systemFontOfSize:15];
    replyVolu.numberOfLines = 0;
    [replyVolu sizeToFit];
    [replyView addSubview:replyVolu];
    
    if (imageView) {
        
        replyView.frame = CGRectMake(0, CGRectGetMaxY(imageView.frame)+10, WIDTH, CGRectGetMaxY(replyVolu.frame)+10);
        
        if (replyVolu.text.length<=0) {
            
            replyView.frame = CGRectMake(0, CGRectGetMaxY(imageView.frame)+10, WIDTH, CGRectGetMaxY(imageView.frame)+10);
        }
      
    }else {
    
          replyView.frame = CGRectMake(0, CGRectGetMaxY(comment.frame)+10, WIDTH, CGRectGetMaxY(replyVolu.frame)+10);
        if (replyVolu.text.length<=0) {
            
            replyView.frame = CGRectMake(0, CGRectGetMaxY(comment.frame)+10, WIDTH, CGRectGetMaxY(comment.frame)+10);
        }
    }
    
    
    
    commentView.frame = CGRectMake(0, 0, WIDTH, CGRectGetMaxY(replyView.frame));
    
    
    
    UIView * priceView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(commentView.frame)+10, WIDTH, 100)];
    priceView.backgroundColor =[UIColor whiteColor];
    
    [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"外卖金额" andFont:17 andAliment:NSTextAlignmentLeft onView:priceView];
    
    [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"￥%@",self.dataDic[@"order_price"]] andFont:16 andAliment:NSTextAlignmentRight onView:priceView];
    
    [self creatLabelFrame:CGRectMake(10, 65, 120, 20) andTextColor:[UIColor blackColor] andText:@"支付方式" andFont:16 andAliment:NSTextAlignmentLeft onView:priceView];
    
    [self creatLabelFrame:CGRectMake(130, 65, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"pay_type_string"]] andFont:16 andAliment:NSTextAlignmentRight onView:priceView];
    
    [footerView addSubview:priceView];
    
    UIView * orderView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(priceView.frame)+10, WIDTH, 100)];
    orderView.backgroundColor =[UIColor whiteColor];
    
    [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"外卖编号" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"order_sn"]] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
    [self creatLabelFrame:CGRectMake(10, 65, 120, 20) andTextColor:[UIColor blackColor] andText:@"外卖变更时间" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(130, 65, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"update_time"]] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
    if ([self.dataDic[@"status"] isEqualToString:@"deliver"]||[self.dataDic[@"status"] isEqualToString:@"got"]) {
        
        UIView * emsView =[[UIView alloc]initWithFrame:CGRectMake(0, 230, WIDTH, 50)];
        emsView.backgroundColor =[UIColor whiteColor];
        
        [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"骑手" andFont:15 andAliment:NSTextAlignmentLeft onView:emsView];
        
        [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@   %@",self.dataDic[@"deliver"][@"user_name"],self.dataDic[@"deliver"][@"phone"]] andFont:15 andAliment:NSTextAlignmentRight onView:emsView];
        
        [footerView addSubview:emsView];
    }
    
    [footerView addSubview:orderView];
    return footerView;
    
}


-(void)creatLabelFrame:(CGRect)rec andTextColor:(UIColor*)col andText:(NSString *)text andFont:(CGFloat)flo andAliment:(NSTextAlignment)aliment onView:(UIView *)view {
    
    
    UILabel * statisLabel =[[UILabel alloc]initWithFrame:rec];
    statisLabel.font = [UIFont systemFontOfSize:flo];
    statisLabel.textColor = col;
    statisLabel.textAlignment = aliment;
    statisLabel.text = text;
    [view addSubview:statisLabel];
    
    
}


@end
